//
//  BlockView.m
//  CoreGraphicsTest
//
//  Created by aewatson on 11/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SpriteView.h"


@implementation SpriteView

@synthesize spriteModel;
@synthesize images;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithSpriteModel:(SpriteModel*) newSpriteModel andImages:(BlockImages *)newImages{
    self = [super init];
    [self setSpriteModel: newSpriteModel];
    [self setImages:newImages];
    
    UIImage *image;
    [self setFrame:[newSpriteModel getRect]];
    switch([spriteModel spriteType]) {
        case TYPE_EXPLOSION: case TYPE_BLOCK: 
            image = [images getBlockImageForColor:[newSpriteModel color] andState:[newSpriteModel state]];
            [self setImage:image];
            break;
        case TYPE_SHOT:
            image = [images getBulletImageForColor:[newSpriteModel color]];
            [self setImage:image];
            break;
    }
    if([spriteModel spriteType] == TYPE_BLOCK) {
        
    }
    else {
        
    }
    
    return self;
}

- (void)updateFromModel {
    [self setCenter:CGPointMake([spriteModel centerX], [spriteModel centerY])];
}

- (void)nextState {
    if([spriteModel state] <= EXPLOSION_STATE_MAX) {
        UIImage *image = [images getBlockImageForColor:[spriteModel color] andState:[spriteModel state]];
        [self setImage:image];
    }
    else {
        [self setImage:NULL];
    }
}

- (void)changeSize {
    // set size & location
    CGRect rect = CGRectMake([spriteModel getLeft], [spriteModel getTop], [spriteModel getWidth], [spriteModel getHeight]);
    [self setFrame:rect];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

@end
